const Controller = require("egg").Controller;

class ClazzController extends Controller {

    //restful: index/create/destroy/update

    //班级，学生；

    async index() {
        // let id = this.ctx.request.query.id
        // let clazzList = await this.app.model.Clazz.findAll({
        //     //通过where条件查询
        //     where:{
        //         id:id
        //     }
        // })
        let list = await this.ctx.service.clazz.getclazzList()
        if (list) {
            this.ctx.body = {
                code: 20000,
                data: list
            }
        } else {
            this.ctx.body = {
                code: 50000,
                msg: "服务器异常，请与管理员联系。"
            }
        }
        this.ctx.body = list
    }
    async create() {
        let name = this.ctx.request.body.name;
        let result = await this.ctx.service.clazz.createClazz(name)
        if (result) {
            this.ctx.body = {
                code: 20000,
                msg: "添加成功"
            }
        } else {
            this.ctx.body = {
                code: 50000,
                msg: "数据添加失败，请与管理员联系。"
            }
        }
        this.ctx.body = "添加成功"
    }
    async destroy() {
        let id = this.ctx.params.id
        let del = await this.ctx.service.student.destroyStudent(id)
        if(del){
            this.ctx.body = {
                code:20000,
                msg:"删除成功"
            }
        }else{
            this.ctx.body = {
                code:50000,
                msg:"删除失败"
            }
        }
        this.ctx.body = "删除班级数据"
    }
    // async update() {
    //     let id = this.ctx.params.id
    //     let name = this.ctx.request.body.name
    //     await this.app.model.Clazz.update({
    //         name: name
    //     }, {
    //         where: {
    //             id: id
    //         }
    //     })
    //     this.ctx.body = "修改成功"
    // }

}

module.exports = ClazzController;